#include "ui.h"


// 检查输入是否存在空格
int check_space(const char *str)
{
    if (str == NULL)
    {
        return 0; // 空指针视为不包含空格
    }

    for (int i = 0; str[i] != '\0'; i++)
    {
        if (isspace((unsigned char)str[i]))
        {
            return 1;
        }
    }
    return 0;
}


// 登录信息录入
int login(UserLogInfo *user)
{
    if (user == NULL)
    {
        printf(RED "错误: 无效参数!\n" RESET);
        return -1; // 无效参数
    }

    char user_name_input[256];
    char user_password_input[256];

    // system("clear");
    drawLoginBanner();
    drawInputBox("请输入用户名(不允许有空格)");

    // 用户名输入循环
    while (1)
    {
        bzero(user_name_input, sizeof(user_name_input));
        if (fgets(user_name_input, sizeof(user_name_input), stdin) == NULL)
        {
            printf(RED "\n输入错误,请重新输入!\n" RESET);
            drawInputBox("请重新输入用户名");
            continue;
        }

        // 去除换行符
        user_name_input[strcspn(user_name_input, "\n")] = '\0';

        // 输入验证
        if (strlen(user_name_input) == 0)
        {
            printf(RED "\n错误：用户名不能为空！\n" RESET);
            drawInputBox("请重新输入用户名");
            continue;
        }

        if (check_space(user_name_input))
        {
            printf(RED "\n错误：用户名不能包含空格！\n" RESET);
            drawInputBox("请重新输入用户名");
            continue;
        }

        if (strlen(user_name_input) >= sizeof(user->user_name))
        {
           printf(RED "\n错误：用户名过长！最大允许%d字符。\n" RESET, 
                  (int)sizeof(user->user_name) - 1);
            drawInputBox("请重新输入用户名");
            continue;
        }

        break; // 输入有效，退出循环
    }

    // 拷贝用户名
    strncpy(user->user_name, user_name_input, sizeof(user->user_name) - 1);
    user->user_name[sizeof(user->user_name) - 1] = '\0'; 

    drawInputBox("请输入密码(不允许有空格)");

    // 密码输入循环
    while (1)
    {
        bzero(user_password_input, sizeof(user_password_input));
        if (fgets(user_password_input, sizeof(user_password_input), stdin) == NULL)
        {
            printf(RED "\n输入错误，请重新输入!\n" RESET);
            drawInputBox("请重新输入密码");
            continue;
        }

        // 去除换行符
        user_password_input[strcspn(user_password_input, "\n")] = '\0';

        // 输入验证
        if (strlen(user_password_input) == 0)
        {
            printf(RED "\n错误：密码不能为空！\n" RESET);
            drawInputBox("请重新输入密码");
            continue;
        }

        if (check_space(user_password_input))
        {
            printf(RED "\n错误：密码不能为空！\n" RESET);
            drawInputBox("请重新输入密码");
            continue;
        }

        if (strlen(user_password_input) >= sizeof(user->password))
        {
            printf(RED "\n错误：密码过长！最大允许%d字符。\n" RESET, 
                  (int)sizeof(user->password) - 1);
            drawInputBox("请重新输入密码");
            continue;
        }

        break; // 输入有效，退出循环
    }

    // 拷贝密码
    strncpy(user->password, user_password_input, sizeof(user->password) - 1);
    user->password[sizeof(user->password) - 1] = '\0'; 

    // 清空输入缓冲区中的敏感信息
    bzero(user_name_input, sizeof(user_name_input));
    bzero(user_password_input, sizeof(user_password_input));

    return 0;
}

int get_register_user_info(UserLogInfo *user)
{
    if (user == NULL)
    {
        printf(RED "错误: 无效参数!\n" RESET);
        return -1; // 无效参数
    }

    char user_name_input[256];
    char user_password1_input[256];
    char user_password2_input[256];

    system("clear");
    drawRegisterBanner();
    drawInputBox("请输入用户名(不允许有空格)");

    // 用户名输入循环
    while (1)
    {
        bzero(user_name_input, sizeof(user_name_input));
        if (fgets(user_name_input, sizeof(user_name_input), stdin) == NULL)
        {
            printf(RED "\n输入错误，请重新输入!\n" RESET);
            drawInputBox("请重新输入用户名");
            continue;
        }

        // 去除换行符
        user_name_input[strcspn(user_name_input, "\n")] = '\0';

        // 输入验证
        if (strlen(user_name_input) == 0)
        {
            printf(RED "\n错误：用户名不能为空！\n" RESET);
            drawInputBox("请重新输入用户名");
            continue;
        }

        if (check_space(user_name_input))
        {
            printf(RED "\n错误：用户名不能包含空格！\n" RESET);
            drawInputBox("请重新输入用户名");
            continue;
        }

        if (strlen(user_name_input) >= sizeof(user->user_name))
        {
            printf(RED "\n错误：用户名过长！最大允许%d字符。\n" RESET, 
                  (int)sizeof(user->user_name) - 1);
            drawInputBox("请重新输入用户名");
            continue;
        }

        break; // 输入有效，退出循环
    }

    // 拷贝用户名
    strncpy(user->user_name, user_name_input, sizeof(user->user_name) - 1);
    user->user_name[sizeof(user->user_name) - 1] = '\0'; // 确保终止符

    // 输入循环
    while (1)
    {
        drawInputBox("请输入密码(不允许有空格)");

        // 第一次密码输入
        while (1)
        {
            bzero(user_password1_input, sizeof(user_password1_input));
            if (fgets(user_password1_input, sizeof(user_password1_input), stdin) == NULL)
            {
                printf(RED "\n输入错误，请重新输入!\n" RESET);
                drawInputBox("请重新输入密码");
                continue;
            }

            // 去除换行符
            user_password1_input[strcspn(user_password1_input, "\n")] = '\0';

            // 输入验证
            if (strlen(user_password1_input) == 0)
            {
                printf(RED "\n错误：密码不能为空！\n" RESET);
                drawInputBox("请重新输入密码");
                continue;
            }

            if (check_space(user_password1_input))
            {
                printf(RED "\n错误：密码不能包含空格！\n" RESET);
                drawInputBox("请重新输入密码");
                continue;
            }

            if (strlen(user_password1_input) >= sizeof(user->password))
            {
                printf(RED "\n错误：密码过长！最大允许%d字符。\n" RESET, 
                      (int)sizeof(user->password) - 1);
                drawInputBox("请重新输入密码");
                continue;
            }

            break; // 输入有效，退出循环
        }

        drawInputBox("请再次输入密码确认");

        // 第二次密码输入
        while (1)
        {
            bzero(user_password2_input, sizeof(user_password2_input));
            if (fgets(user_password2_input, sizeof(user_password2_input), stdin) == NULL)
            {
                printf(RED "\n输入错误，请重新输入!\n" RESET);
                drawInputBox("请重新输入确认密码");
                continue;
            }

            // 去除换行符
            user_password2_input[strcspn(user_password2_input, "\n")] = '\0';

            // 输入验证
            if (strlen(user_password2_input) == 0)
            {
                printf(RED "\n错误：密码不能为空！\n" RESET);
                drawInputBox("请重新输入确认密码");
                continue;
            }

            if (check_space(user_password2_input))
            {
                printf(RED "\n错误：密码不能为空！\n" RESET);
                drawInputBox("请重新输入确认密码");
                continue;
            }

            break; // 输入有效，退出循环
        }

        // 检查两次密码是否一致
        if (strcmp(user_password1_input, user_password2_input) != 0)
        {
            printf(RED "\n两次密码输入不一致，请重新输入!\n" RESET);
            continue;
        }

        break; // 密码验证通过，退出循环
    }

    // 安全拷贝密码
    strncpy(user->password, user_password1_input, sizeof(user->password) - 1);
    user->password[sizeof(user->password) - 1] = '\0'; // 确保终止符

    // 清空输入缓冲区中的敏感信息
    bzero(user_name_input, sizeof(user_name_input));
    bzero(user_password1_input, sizeof(user_password1_input));
    bzero(user_password2_input, sizeof(user_password2_input));

    return 0;
}

int display_menu(ClientContext *ctx, UserLogInfo *user)
{
    if (ctx == NULL || user == NULL)
    {
        return -1; // 无效参数
    }

    int choice;
    while (1)
    {
        // system("clear");
        drawBanner();
        drawMenuBorder();
        drawMenuItem("1. 用户登录");
        drawMenuItem("2. 用户注册");
        drawMenuItem("3. 退出系统");
        drawMenuBorder();
        
        printf(GREEN "\nn请选择操作 (1-3): " RESET);
        
        if (scanf("%d", &choice) != 1) {
            while (getchar() != '\n');
            printf(RED "\n错误: 请输入有效数字!\n" RESET);
            sleep(1); // 暂停1秒让用户看到错误信息
            continue;
        }

        // 清空输入缓冲区中的剩余字符
        while (getchar() != '\n')
            ;

        // 处理用户选择
        switch (choice)
        {
        case 1:
            if (login(user) == 0)
            {
                printf("登录信息已发送,正在处理...\n");
                if (login_user(ctx, user) == 0)
                {
                    printf(GREEN "\n登录成功!\n" RESET);
                    sleep(1);
                    return STATE_AWAIT_RESP;
                }
                else
                {
                    printf(RED "\n登录失败,请重试!\n" RESET);
                    sleep(1);
                }
                
            }
            break;
        case 2:
            if (get_register_user_info(user) == 0)
            {
                if (register_user(ctx, user) == 0)
                {
                    printf(GREEN "\n注册成功!\n" RESET);
                    sleep(1);
                }
                else
                {
                    printf(RED "\n注册失败,请重试!\n" RESET);
                    sleep(5);
                }
            }
            break;
        case 3:
            printf(CYAN "\n退出系统,再见！\n" RESET);
            return EXIT; // 正常退出
        default:
            printf(RED "\n输入无效,请输入1、2或3!\n" RESET);
            sleep(1);
        }
    }
}


